package com.example.administrator.wallet.adapter;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.model.bean.MainBean;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.fragment.TransactionFragment;

import java.util.List;

/**
 * Effect:
 * <p>
 * Author:  CaiYapeng
 * <p>
 * Time  :  2018/8/9.
 */
public class CoinChangeAdapter extends BaseAdapter {
    private int pst;
    private Activity context;
    private List<MainBean.DataBean.ListBean> changeData;
    private ChangeSelect position;

    public CoinChangeAdapter(FragmentActivity activity, List<MainBean.DataBean.ListBean> changeData, int pst) {
        this.context = activity;
        this.changeData = changeData;
        this.pst = pst;
    }

    @Override
    public int getCount() {
        return changeData == null ? 0 : changeData.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final MainBean.DataBean.ListBean iData = changeData.get(i);

        //设置标签，进行标记
        final ViewHolder holder;
        if (view == null) {
            view = View.inflate(context, R.layout.item_coin_change, null);
            holder = new ViewHolder();
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        //查找控件
        holder.coin_icon = (ImageView) view.findViewById(R.id.coin_icon);
        holder.coin_name = (TextView) view.findViewById(R.id.coin_name);
        holder.coin_select = (ImageView) view.findViewById(R.id.coin_select);
        holder.mLinear = (LinearLayout) view.findViewById(R.id.mLinear);

        //设置数据
        holder.coin_name.setText(iData.getCoin_symbol());
        Glide.with(context).load(iData.getIcon()).into(holder.coin_icon);

        if (pst == i) {
            holder.coin_select.setVisibility(View.VISIBLE);
            holder.coin_select.setImageResource(R.drawable.icon_xuanzhong);
        }else{
            holder.coin_select.setVisibility(View.GONE);
        }

        //点击事件
        holder.mLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPrefsUtil.putValue(context, "wallet", "position", i);
                TransactionFragment.position=i;
                TransactionFragment.changeAdapter.notifyDataSetChanged();
                position.select(iData);
            }
        });
        return view;
    }


    private class ViewHolder {
        private ImageView coin_icon;
        private TextView coin_name;
        private ImageView coin_select;
        private LinearLayout mLinear;
    }

    public interface ChangeSelect {
        void select(MainBean.DataBean.ListBean coin_data);
    }

    public void onClickView(ChangeSelect position) {
        this.position = position;
    }
}
